package com.yanggu.mapreduce.customizeInputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/6 13:12
 * @description:
 */
public class CustomizeInputFormatDriver {

    public static void main(String[] args) throws Exception {
        args = new String[]{"input/customizeInputformat", "output/customizeInputformat"};

        //1. 获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2. 设置关联driver、mapper、reducer类
        job.setJarByClass(CustomizeInputFormatDriver.class);
        job.setMapperClass(CustomizeInputFormatMapper.class);
        job.setReducerClass(CustomizeInputFormatReducer.class);

        //3. 设置mapper的key和value输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(BytesWritable.class);

        //4. 设置最终的输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(BytesWritable.class);

        //5. 设置FileInputFormat的类型
        job.setInputFormatClass(CustomizeInputFormat.class);

        //6. 设置FileOutPutFormat的类型
        job.setOutputFormatClass(SequenceFileOutputFormat.class);

        //7. 设置输出和输出的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //8. 提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);

    }

}
